package cn.chimade.mom.dict.maintenance.controller;

import java.util.Date;
import java.util.Locale;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import cn.chimade.mom.dict.maintenance.model.SysDic;
import cn.chimade.mom.dict.maintenance.service.ISysDicService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
/**
 * <p>
 * 数据字典明细表 前端控制器
 * </p>
 * @author Miller
 * @since 2017-07-18
 */
@Controller
@RequestMapping("VIEW")
public class SysDicController {
	@Autowired
	private ISysDicService sysDicService;
	private static final Logger log = LoggerFactory.getLogger(SysDicController.class);
	@RenderMapping
	public String view(Locale locale, Model model) {
		return "view";  
	}
	@RenderMapping(params = "action=toAdd")
	public String toAdd(Model model){
 		return "add";
	}
	@RenderMapping(params = "action=toEdit")
	public String toEdit(SysDic sysDict,Model model,RenderRequest request,RenderResponse response){
 		Wrapper<SysDic> wrapper = new EntityWrapper<SysDic>();
        wrapper = wrapper.eq("id", sysDict.getId()); 
 		model.addAttribute("sysDictModel", sysDicService.selectObj(wrapper));
 		return "edit";
	}
	/***
	 * 列出所有的字典信息
	 * @param session
	 * @param request
	 * @param response
	 * @throws Exception
	 */    
	@ResourceMapping(value = "listSysDict")
	public @ResponseBody void listSysDict(Page<SysDic> page, ResourceRequest request, ResourceResponse response) throws Exception {
 		String searchName = ParamUtil.getString(request, "searchName",null);
        page = sysDicService.selectPage(page, null);
   		String json = JSONFactoryUtil.looseSerializeDeep(page);
 		response.getWriter().write(json);
 	} 
	/** 
	 * 更新查看 编辑字典信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@ResourceMapping(value = "editSysDict")
	public @ResponseBody void editSysDict(SysDic sysDict,ResourceRequest request, ResourceResponse response) throws Exception {
		String results = "";
		boolean rlt = false;
		//sysDict.setId(CounterLocalServiceUtil.increment(SysDic.class.getName()));
		if ( sysDict != null ) {
			if ( sysDict.getId() != null ) {
				sysDict.setUpdateBy(PortalUtil.getUser(request).getUserId());
				sysDict.setUpdateTime(new Date());
				rlt = sysDicService.updateById(sysDict);
			} else {
				sysDict.setCreateBy(PortalUtil.getUser(request).getUserId());
				sysDict.setCreateTime(new Date());
				rlt = sysDicService.insert(sysDict);
			}
		}
 		if(rlt){
			results ="success";
		}else{
			results ="error";
		}   
		response.setContentType("application/json");
		response.getWriter().write(results);
	} 
 
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@ResourceMapping(value = "deleteCategory")
	public @ResponseBody void deleteCategory(ResourceRequest request, ResourceResponse response) throws Exception {
		String results = "";
 		String ids = ParamUtil.getString(request, "ids");
  		 String arr[] = ids.split(",");
 		 for(String id : arr){
 			 if( !"on".equals(id) && ! "".equals(id)){
 				sysDicService.deleteById(id);
 			 }
 		 }
 	    results ="success";
		response.setContentType("application/json");
		response.getWriter().write(results);
	}
}
